from django.shortcuts import render
from django.shortcuts import redirect
from django.views.generic import TemplateView

from .forms import CommentForm
# Create your views here.

class CommentView(TemplateView):
    http_method_names=['post']
    template_name='comment/result.html'
    
    def post(self,request,*args,**kwargs):
        comment_form=CommentForm(request.POST)
        target=request.POST.get('target')

        if comment_form.is_valid():
            instance=comment_form.save(commit=False)
            instance.target=target
            instance.save()
            succeed=True
            return redirect(target)
        else:
            succeed=False
        
        context={
            'succeed':succeed,
            'form':comment_form,
            'target':target,
        }
        return self.render_to_response(context)
        #这里直接使用TemplateView来完成，这个View只提供了POST方法。其逻辑是通过CommentForm来处理接收的数据，
        #然后验证并保存。最后渲染评论结果页，如果中间有校验失败的部分，也会展示到评论结果页。接下来要在选定的
        #主题模板中增加comment/result.html文件